package com.demonzym.framework.application;

import com.demonzym.framework.net.HttpEngin;

import android.app.Application;
import android.os.Handler;
import android.os.Process;
import android.util.Log;

/**
 * 
 */
public class MyApplication extends Application {
	private final String TAG = "App";
	
	private static MyApplication sApp;
	
	public static HttpEngin mHttpEngin = new HttpEngin();
	
	static public MyApplication getAppInstance(){
		if(sApp == null)
			throw new NullPointerException("app not create or be terminated!");
		return sApp;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
				 
		sApp = this;
	}
	
	@Override
	public void onTerminate() {
		super.onTerminate();
	}
		
	public void finishApp(){
		
		new Handler().postDelayed(new Runnable() {
			
			@Override
			public void run() {
				KillApplication.killApplicationByPackageName(MyApplication.this, getPackageName());
			}
		}, 1000);

	}
	
	
	public class WbUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler{

		@Override
		public void uncaughtException(Thread thread, Throwable ex) {
			Log.i("WbUncaughtExceptionHandler", "WbUncaughtExceptionHandler");
			ex.printStackTrace();
			
			finishApp();
			
			Process.killProcess(Process.myPid());
		}
		
	}
}
